<?php

namespace api\controllers;

use common\core\actions\GetAction;
use common\core\actions\PostAction;
use dmstr\widgets\Menu;
use mdm\admin\components\MenuHelper;
use server\UserInterface;
use server\RoleInterface;
use Yii;
use yii\base\Module;

class RoleController extends Controller
{
    public $user;
    public $user_role;

    public function __construct($id, Module $module, UserInterface $user, RoleInterface $user_role, array $config = [])
    {
        $this->user = $user;
        $this->user_role = $user_role;
        parent::__construct($id, $module, $config);
    }

    public function actions()
    {
        return [
            'index' => [
                'class' => GetAction::class,
                'data' => function () {
                    return $this->user_role->getRoleList();
                },
            ],
            'assignment' => [
                'class' => PostAction::class,
                'validateFiled' => [
                    'roles',
                    'user_id',
                ],
                'data' => function ($data) {
                   return  $this->user_role->assign($data);
                },
            ],
            'permission' => [
                'class' => GetAction::class,
                'data' => function () {
                    return $this->user_role->getPermissionList();
                },
            ],
            'menu' => [
                'class' => GetAction::class,
                'data' => function () {
                    $callback = function ($menu) {
                        $data = json_decode($menu['data'], true);
                        $items = $menu['children'];
                        $return = [
                            'label' => $menu['name'],
                            'url' => [$menu['route']],
                        ];
                        //处理我们的配置
                        if ($data) {
                            //visible
                            isset($data['visible']) && $return['visible'] = $data['visible'];
                            //icon
                            isset($data['icon']) && $data['icon'] && $return['icon'] = $data['icon'];
                            //other attribute e.g. class...
                            $return['options'] = $data;
                        }
                        //没配置图标的显示默认图标，默认图标大家可以自己随便修改
                        (!isset($return['icon']) || !$return['icon']) && $return['icon'] = 'circle-o';
                        $items && $return['items'] = $items;

                        return $return;
                    };
                    return  MenuHelper::getAssignedMenu(Yii::$app->user->id, null, $callback);
                    echo Menu::widget([
                        'options' => ['class' => 'sidebar-menu tree', 'data-widget' => 'tree'],
                        'items' => MenuHelper::getAssignedMenu(Yii::$app->user->id, null, $callback),
                    ]);
                },
            ],


        ];
    }


}
